package org.lwz.space.service.impl;

import org.lwz.space.dao.ArticleDao;
import org.lwz.space.dao.ArticleTypeDao;
import org.lwz.space.model.Article;
import org.lwz.space.model.ArticleType;
import org.lwz.space.service.ArticleService;
import org.lwz.space.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Liaowz on 2015/10/16.
 */
@Service("articleTypeService")
public class ArticleTypeServiceImpl implements ArticleTypeService {

    @Autowired
    private ArticleTypeDao articleTypeDao;

    @Autowired
    private ArticleDao articleDao;

    public void save(ArticleType articleType) {
        this.articleTypeDao.save(articleType);
    }

    public void delete(ArticleType articleType) {
        //TODO:用下面的实现
        List<Article> articles = articleDao.listBy(articleType);
        for (Article article : articles) {
            articleDao.delete(article);
        }
        articleTypeDao.deleteByName(articleType.getName());


//        List<Article> articleList = articleDao.list();
//            List<Article> deleteArticleList = new LinkedList<Article>();
//            for(Article article : articleList){
//                if((article.getArticleType().getName()).equals(articleType.getName())){
//                    deleteArticleList.add(article);
//                }
//            }
//            for(Article deleteArticle : deleteArticleList){
//                articleDao.delete(deleteArticle);
//            }
//            articleTypeDao.delete(articleType);
    }

    @Override
    public ArticleType getArticleType(String name) {
        return this.articleTypeDao.getArticleType(name);
    }

    public List<ArticleType> list() {
        return this.articleTypeDao.list();
    }

    public void update(ArticleType articleType) {
        this.articleTypeDao.update(articleType);//TODO: 错，参照Update Use怎么更新的
    }


    public void setArticleTypeDao(ArticleTypeDao articleTypeDao) {
        this.articleTypeDao = articleTypeDao;
    }

    public void setArticleDao(ArticleDao articleDao) {
        this.articleDao = articleDao;
    }
}
